const utils = {
    /**
     * 时间戳转年月日
     * @param {时间戳} timestamp 
     * @returns 
     */
    getDateYYMMDD(timestamp) {
        var time = new Date(Number(timestamp));
        var year = time.getFullYear();
        var month = time.getMonth() + 1;
        var date = time.getDate();
        return year + '年' + this.add0(month) + '月' + this.add0(date) + '日';
    },
    add0(m) {
        return m < 10 ? '0' + m : m;
    },
    /**
     * 时间戳转年月日 时分秒
     * @param {时间戳} timestamp 
     * @returns 
     */
    getDateYYMMDDTime(timestamp) {
        var time = new Date(Number(timestamp));
        var year = time.getFullYear();
        var month = time.getMonth() + 1;
        var date = time.getDate();
        var hours = time.getHours();
        var minutes = time.getMinutes();
        var seconds = time.getSeconds();
        return year + '年' + this.add0(month) + '月' + this.add0(date) + '日' + ' ' + this.add0(hours) + ':' + this.add0(minutes) + ':' + this.add0(seconds);
    },
    $getDateYYMMDDT(timestamp) {
        var time = new Date(Number(timestamp));
        var year = time.getFullYear();
        var month = time.getMonth() + 1;
        var date = time.getDate();
        var hours = time.getHours();
        var minutes = time.getMinutes();
        var seconds = time.getSeconds();
        return year + '-' + this.add0(month) + '-' + this.add0(date) + ' ' + this.add0(hours) + ':' + this.add0(minutes) + ':' + this.add0(seconds);
    },
    /**
     * 防抖
     * （funname, waitT）：函数名，等待的时间
     */
    antShake(funname, waitT) {
        var timer;
        return function (e) {
            if (timer) {
                clearTimeout(timer)
                timer = setTimeout(() => {
                    funname.call(this, e);
                }, waitT);
            } else {
                timer = setTimeout(() => {
                    funname.call(this, e);
                }, waitT);
            }
        }
    },
    /**
     * 中国标准时间转时间戳
     */
    $getTimeStamp(str) {
        return new Date(str).getTime()
    },

    /**
     * 
     * @param {*} arrObj 数组对象
     * @param {*} key 需判断的键名
     */
    $delEmptyObj(arrObj, key) {
        let mydata = [];
        arrObj.forEach((el) => {
            if (el[key] != null || el[key] != 0) {
                mydata.push(el)
            }
        });
        return mydata;
    },
    /**
     * 下载图片到本地
     * @param {*} imgClassNimgNameame 需要下载的图片标签的 类名
     * @param {*} imgName 保存的名字
     */
    $downloadImage(imgClassName, imgName) {
        // 将链接地址字符内容转变成blob地址, 解决图片下载直接打开问题
        // url 为图片地址
        console.log(imgClassName);
        var img = document.querySelector(`.${imgClassName}`); // 获取要下载的图片
        var url = img.src;
        fetch(url).then(async res => await res.blob()).then((blob) => {
            // 创建隐藏的可下载链接
            const a = document.createElement('a');
            a.style.display = 'none';
            a.href = URL.createObjectURL(blob);
            // 保存下来的文件名
            a.download = `${imgName}.png`;
            document.body.appendChild(a);
            a.click();
            // 移除元素
            document.body.removeChild(a);
        })
    },
}
export default utils;